<?php
namespace app\admin\model;

use think\Model;

class OrderMake extends Base
{
    protected $name = 'order_make';

    // 获取预约列表
    public function getMakeList($where = [], $curr = 1, $limit = 20)
    {
        try{
            $adminModel = new Admin();
            $usersModel = new Users();
            $goodsModel = new Goods();
            $technTimeModel = new TechnTime();

            $field = 'id, admin_id, user_id, goods_id, techn_user_id, techn_time_id, status, create_time, make_time';
            $page = ($curr - 1) * $limit;
            $list = $this->where($where)->field($field)->limit($page, $limit)->select();

            if($list->isEmpty()){
                return ['code' => 'fail','data' => []];
            }

            foreach($list as &$v){
                $v['shop_name']  = $adminModel->getVal(['id' => $v['admin_id']], 'nickname')['data'];
                $v['user_name']  = $usersModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
                $v['goods_name'] = $goodsModel->getVal(['id' => $v['goods_id']], 'title')['data'];
                $v['techn_name'] = $usersModel->getVal(['id' => $v['techn_user_id']], 'nickname')['data'];
                $v['time_name']  = date('Y-m-d', $v['make_time']) . ' ' . $technTimeModel->getVal(['id' => $v['techn_time_id']], 'time_name')['data'];
                unset($v['admin_id']);
                unset($v['user_id']);
                unset($v['goods_id']);
                unset($v['techn_user_id']);
                unset($v['techn_time_id']);
            }

            return ['code' => 'ok', 'data' => $list];
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage()];
        }
    }

    public function getStatusAttr($val){
        $status = [1 => '已预约', 2 => '已完成', 3 => '已取消', 4 => '已到期'];
        return $status[$val];
    }

    public function getPayTimeAttr($val){
        if(!$val){
            return '-';
        }

        return date('Y-m-d H:i:s', $val);
    }

    public function getEndTimeAttr($val){
        if(!$val){
            return '-';
        }

        return date('Y-m-d H:i:s', $val);
    }
}